<template>
  <div id="dig">

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>挖矿收益配置</span>
      </div>
      <div v-for="(item,key) in listdata" :key="key" class="listbox">
        <el-row>
          <el-col :span="3">
            <div class="grid-content ">{{ item.day }}天</div>
          </el-col>
          <el-col :span="5">
            <div class="grid-content ">收益 <div><el-input v-model="item.rewardRate" style="width: 100px;" placeholder="" /> %</div> </div>
          </el-col>
          <el-col :span="9">
            <div class="grid-content ">年化率

              <div>  <el-input v-model="item.lowerLimit" style="width: 100px;" placeholder="" @input="item.lowerLimit=setnumber(item.lowerLimit)" /> % </div>           ~
              <div> <el-input v-model="item.upperLimit" style="width: 100px;" placeholder="" @input="item.upperLimit=setnumber(item.upperLimit)" />% </div>

            </div>
          </el-col>
          <el-col :span="7">
            <div class="grid-content"><el-button type="primary" size="min" @click="okseve(item)">确定保存</el-button></div>
          </el-col>

        </el-row>
        <el-divider />
      </div>

    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>注：公司收益和运维收益比例设置，只是为了数据统计，不做实际转账操作。</span>
      </div>
      <div>
        <el-row>
          <el-col :span="5">
            <div class="">
              <!-- <div>公司收益</div> -->
              <div class="grid-content ">公司收益: <div><el-input v-model="params.yw" style="width: 100px;" placeholder="" @input="params.yw=setnumber(params.yw)" /> %</div> </div>
            </div>
          </el-col>
          <el-col :span="5">
            <!-- <div>运维收益</div> -->
            <div class="grid-content ">运维收益: <div><el-input v-model="params.gs" style="width: 100px;" placeholder="" @input="params.gs=setnumber(params.gs)" /> %</div> </div>
          </el-col>

          <el-col :span="14">
            <div class="grid-content"><el-button type="primary" size="min" @click="okseve1()">确定保存</el-button></div>
          </el-col>

        </el-row>
        <!-- <el-divider></el-divider> -->
      </div>

    </el-card>
  </div>
</template>
<script>
import { getdiglist, setdiglist, getincomelist, setincomelist } from '@/api/data1.js'
export default {
  name: 'Dig',
  data() {
    return {
      params: {
        yw: '',
        gs: ''
      },
      listdata: [
        // {title:'1',value:'',n1:"",n2:"",},
        // {title:'7',value:'',n1:"",n2:"",},
        // {title:'15',value:'',n1:"",n2:"",},
        // {title:'20',value:'',n1:"",n2:"",}
      ]

    }
  },
  mounted() {
    this.getlist()
    this.getlist1()
  },
  methods: {
    setnumber(params) {
      params = params || 0
      if (params == '') {
        return
      }
      params = params.replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
      params = params.replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
      params = params.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
      params = params.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')// 只能输入两个小数
      if (params.indexOf('.') < 0 && params != '') { // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        params = parseFloat(params)
      }
      if (params >= 100) {
        params = 100
      }
      if (params < 0) {
        params = 0
      }
      return params
      // console.log(price)
    },

    async getlist() {
      const { data, code } = await getdiglist()

      if (code == 0) {
        data.forEach(params => {
          // params.lowerLimit = params.lowerLimit * 100
          // params.rewardRate = params.rewardRate * 100
          // params.upperLimit = params.upperLimit * 100
        })
        this.listdata = data
      }
    },

    async okseve(params) {
      const res = await setdiglist(params)
      if (res.code == 0) {
        this.$message('保存成功')
        this.getlist()
      }
      // console.log(obj)
      // this.$message('我是确定')
    },
    async getlist1() {
      const { data, code } = await getincomelist()

      if (code == 0) {
        this.params.gs = data[0].confValue
        this.params.yw = data[1].confValue
        // this.listdata = data
      }
    },

    async okseve(params) {
      // params.lowerLimit = params.lowerLimit / 100
      // params.rewardRate = params.rewardRate / 100
      // params.upperLimit = params.upperLimit / 100

      // return
      const res = await setdiglist(params)
      if (res.code == 0) {
        this.$message('保存成功')

        this.getlist()
      }
      console.log(obj)
      // this.$message('我是确定')
    },
    async okseve1() {
      const params = {
        income: [
          {
            'amount': this.params.yw  || '',
            'type': 2
          },
          {
            'amount': this.params.gs  || '',
            'type': 1
          }
        ]
      }
      console.log(params)
      // return;
      const res = await setincomelist(params)
      if (res.code == 0) {
        this.getlist1()
        this.$message('保存成功')
      }
    }
  }
}
</script>
<style lang="scss" >
  .box-card{
    width: 97%;
    margin: 0 auto;
    margin-top: 20px;
  }
  #dig{
    .listbox{
      /* border-bottom: 1px solid #dddddd56; */
      margin-top: 40px;
    }
  }
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>

</style>
